package com.shengqiang.cloud.ups.service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.shengqiang.cloud.ups.common.model.JsonResult;
import com.shengqiang.cloud.ups.dao.modal.UpmsPermission;
import com.shengqiang.cloud.ups.dao.repository.master.UpmsPermissionMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2017/12/31.
 */
@Service
public class UpmsResourceWebService extends ServiceImpl<UpmsPermissionMapper,UpmsPermission>{

    private static final Logger logger= LoggerFactory.getLogger(UpmsResourceWebService.class);

    public JsonResult<List<UpmsPermission>> selectResources(int menuId){
        JsonResult<List<UpmsPermission>> result=null;
        try{
            List<UpmsPermission> list=baseMapper.selectResourcesByPId(menuId);
            if(list==null){
                result= new JsonResult<>(1,"查询资源列表失败");
            }else{
                result =new JsonResult<>(0,"sucess");
                result.setData(list);

            }
        }catch (Exception ex){
            logger.error("查询菜单资源信息列表出现异常:{}",ex.getMessage(),ex);
            result= new JsonResult<>(1,"查询资源列表失败");
        }
        return result;
    }

    public JsonResult addSysResource(UpmsPermission upmsPermission){
        JsonResult result=null;
        try{
            int count=baseMapper.insert(upmsPermission);
            if(count>0){
                result=new JsonResult(0,"添加资源【"+ upmsPermission.getName()+"】成功");
            }else{
                result=new JsonResult(1,"添加资源【"+ upmsPermission.getName()+"】失败");
            }
        }catch (Exception ex){
            logger.error("添加资源【"+ upmsPermission.getName()+"】失败:{}",ex.getMessage(),ex);
            result=new JsonResult(1,"添加资源【"+ upmsPermission.getName()+"】失败");
        }
        return result;
    }

    public JsonResult updateResource(UpmsPermission upmsPermission){

        JsonResult result=null;
        try{
            int count=baseMapper.updateById(upmsPermission);
            if(count>0){
                result=new JsonResult(0,"更新资源【"+ upmsPermission.getName()+"】成功");
            }else{
                result=new JsonResult(1,"更新资源【"+ upmsPermission.getName()+"】失败");
            }
        }catch (Exception ex){
            logger.error("更新资源【"+ upmsPermission.getName()+"】失败:{}",ex.getMessage(),ex);
            result=new JsonResult(1,"更新资源【"+ upmsPermission.getName()+"】失败");
        }
        return result;
    }

    public JsonResult deleteSysResource(int id){
        JsonResult result=null;
        try{
            int count=baseMapper.deleteById(id);
            if(count>0){
                result=new JsonResult(0,"删除成功");
            }else{
                result=new JsonResult(1,"删除失败");
            }
        }catch (Exception ex){
            logger.error("删除菜单资源出现异常:{}",ex.getMessage(),ex);
            result=new JsonResult(1,"删除失败");
        }
        return result;
    }
}
